<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入品牌名称" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getTableData">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="openDialog">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="品牌名称" prop="name" align="center" />
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button size="small" icon="el-icon-edit" type="text" @click="handleUpdate(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getTableData" />

    <el-dialog :title="dialogText" :visible.sync="dialogVisible" width="500px">
      <el-form :model="dialogParams" ref="dialogForm" :rules="rules">
        <el-form-item label="品牌名称" prop="name">
          <el-input v-model="dialogParams.name" placeholder="请输入品牌名称" clearable />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button :loading="loading" type="primary" @click="confirmDialog('dialogForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMobileBrandTable, addMobileBrandInfo, editMobileBrandInfo } from "@/api/configuration/phoneBrand";
export default {
  name: "phoneBrand",
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      loading: false,
      tableData: [],
      total: 0,
      rules: {
        name: [
          { required: true, message: "请输入品牌名称", trigger: "blur" }
        ],
      },
      dialogText: '新增',
      dialogVisible: false,
      dialogParams: {}
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      this.loading = true
      const { data } = await getMobileBrandTable(this.queryParams)
      this.loading = false
      this.tableData = data.list
      this.total = data.total
    },
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10
      }
      this.getTableData()
    },
    openDialog() {
      this.dialogParams = {}
      this.dialogText = '新增'
      this.dialogVisible = true
    },
    handleUpdate(data) {
      this.dialogParams = data
      this.dialogText = '编辑'
      this.dialogVisible = true
    },
    confirmDialog(data) {
      this.$refs[data].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.dialogText == '新增') {
            addMobileBrandInfo(this.dialogParams).then(res => {
              this.dialogVisible = false
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getTableData()
              this.loading = false
            }).catch(f => {
              this.loading = false
            });
          } else if (this.dialogText == '编辑') {
            editMobileBrandInfo(this.dialogParams).then(res => {
              this.dialogVisible = false
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getTableData()
              this.loading = false
            }).catch(f => {
              this.loading = false
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>
<style lang="scss" scoped>
</style>